﻿using Modbus.Device;
using SqlSugar.Extensions;
using System.IO.Ports;
using WinFormium;
using WinFormium.JavaScript;

namespace TestPressure
{
    internal class WindowBindObject: JavaScriptWindowBindingObject
    {


        public override string Name => "WindowBindObject";
        public override string JavaScriptWindowBindingCode => """"
           var WindowBindObject = {
            showForm: function (message) {
                native function ShowForm();
                return ShowForm(message);
             }
           };
        """";

        public WindowBindObject()
        {
            // 注册同步方法，带有Formium参数的方法表示这个方法行在主进程中，不带Formium参数的方法表示这个方法运行在渲染进程中。
            // Registering synchronous methods. Methods with the Formium parameter indicate that they run in the main process, while methods without the Formium parameter indicate that they run in the rendering process.
            RegisterSynchronousNativeFunction(ShowForm);
        }

        public JavaScriptValue? ShowForm(Formium sender, JavaScriptArray arguments)
        {

            var stringArg = arguments.FirstOrDefault(x => x.ValueType == JavaScriptValueType.String)?.GetString() ?? string.Empty;

            if (stringArg.Length == 0)
            {
               Alert.Error("参数为空，无法跳转",sender);
            }
            else
            {
                if (stringArg == "DeviceManage")
                {
                    DeviceManageForm frm = new DeviceManageForm();
                    frm.ShowDialog();
                }
                else if (stringArg == "TemplateManage")
                {
                    TemplateManageForm frm = new TemplateManageForm();
                    frm.ShowDialog();
                }
                else if (stringArg == "ProjectManage")
                {
                    ProjectManageForm frm = new ProjectManageForm();
                    frm.ShowDialog();
                }
                else if (stringArg == "FormPointManage")
                {
                    FormPointManageForm frm = new FormPointManageForm();
                    frm.ShowDialog();
                }
                else
                {
                    Alert.Info(stringArg, sender);
                }
            }

            return null;
        }
    }
}
